package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {
    private Courier courier=new Courier();
    @Override
    public Courier getModel() {
        return courier;
    }
    @Autowired
    private CourierService courierService;

    @Action(value = "courier_save",
            results = {@Result(name = SUCCESS,
                                location = "./pages/base/courier.html",
                                type = "redirect")})
    public String save(){
        courierService.save(courier);
        return SUCCESS;
    }

    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Action(value = "courier_pageQuery",
            results = {@Result(name = SUCCESS,
                                type = "json")})
    public String pageQuery(){
        PageRequest pageRequest = new PageRequest(page - 1, rows);
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                ArrayList<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(courier.getCourierNum())){
                    Predicate p1 = cb.like(root.get("courierNum").as(String.class),
                            "%" + courier.getCourierNum() + "%");
                    predicates.add(p1);
                }
                if (StringUtils.isNotBlank(courier.getCompany())){
                    Predicate p2 = cb.like(root.get("company").as(String.class),
                            "%"+courier.getCompany()+"%");
                    predicates.add(p2);
                }
                if (StringUtils.isNotBlank(courier.getType())){
                    Predicate p3 = cb.like(root.<String>get("type").as(String.class),
                            "%" + courier.getType() + "%");
                    predicates.add(p3);
                }
                Join<Object, Object> standard = root.join("standard", JoinType.INNER);
                if (courier.getStandard() != null &&
                        StringUtils.isNotBlank(courier.getStandard().getName())){
                    Predicate p4 = cb.like(standard.<String>get("name").as(String.class),
                            "%" + courier.getStandard().getName() + "%");
                    predicates.add(p4);
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        Page<Courier> pageData = courierService.findPageData( specification,pageRequest);
        Map<String, Object> result = new HashMap<>();
        result.put("total",pageData.getTotalElements());
        result.put("rows",pageData.getContent());

        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    @Action(value = "courier_delBatch",
            results = {@Result(name = SUCCESS,
                    location = "./pages/base/courier.html",
                    type = "redirect")})
    public String delBatch(){
        String[] idArray = ids.split(",");
        courierService.delBatch(idArray);
        return SUCCESS;
    }
    private String rid;

    public void setRid(String rid) {
        this.rid = rid;
    }

    @Action(value = "courier_doRestore",
            results = {@Result(name = SUCCESS,
                    location = "./pages/base/courier.html",
                    type = "redirect")})
    public String doRestore(){
        String[] idArray = rid.split(",");
        courierService.doRestore(idArray);
        return SUCCESS;
    }

}
